//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

using System;
using System.Collections.Generic;
using System.Text;

namespace Microsoft.Research.DataLayer
{
    /// <summary>
    /// Partial class implementation of TypeInitializer.
    /// </summary>
    public partial class TypeInitializer
    {
        #region CommonSearches
        /// <summary>
        /// Common Searches provided.
        /// </summary>
        public sealed class CommonSearches
        {
            /// <summary>
            /// Prevents a default instance of the <see cref="CommonSearches"/> class from being created.
            /// </summary>
            private CommonSearches()
            {
            }

            /// <summary>
            /// Check if custom type initializer is already present in registry.
            /// </summary>
            /// <param name="typeClass">the type of the custom type initializer to find.</param>
            /// <param name="connection">the registry connection.</param>
            /// <returns>Returns the custom type initializer found in the registry.</returns>
            public static TypeInitializer FindCustomTypesInRegistry(string typeClass, Connection connection)
            {
                if (string.IsNullOrEmpty(typeClass))
                {
                    throw new ArgumentNullException("typeClass");
                }

                if (null == connection)
                {
                    throw new ArgumentNullException("connection");
                }

                TypeInitializer regType = null;
                try
                {
                    TypeInitializer.ISearch searchQuery = TypeInitializer.ISearch.Create();
                    searchQuery.Query = TypeInitializer.ISearch.AND(
                        TypeInitializer.ISearch.ClassName(StringField.Condition.Equals, typeClass),
                        TypeInitializer.ISearch.IsDeleted(BooleanField.Condition.Equals, false));
                    List<TypeInitializer> typeInitializerList = TypeInitializer.Search(searchQuery, connection);
                    if (typeInitializerList != null && typeInitializerList.Count > 0)
                    {
                        regType = typeInitializerList[0];
                    }
                }
                catch (RecordNotFoundException)
                {
                }

                return regType;
            }

            /// <summary>
            /// Retrieve the available type initializers.
            /// </summary>
            /// <returns>List of all the type initializers present in the registry.</returns>
            public static IEnumerable<TypeInitializer> GetAvailableTypeInitializers(Connection connection)
            {
                if (null == connection)
                {
                    throw new ArgumentNullException("connection");
                }

                IEnumerable<TypeInitializer> types = null;
                try
                {
                    TypeInitializer.ISearch searchQuery = TypeInitializer.ISearch.Create();
                    searchQuery.Query = TypeInitializer.ISearch.IsDeleted(BooleanField.Condition.Equals, false);
                    types = TypeInitializer.Search(searchQuery, connection);
                }
                catch (RecordNotFoundException)
                {
                    throw;
                }
                return types;
            }
        }
        #endregion
    }
}
